// pages/share/share.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const jumpWeapp = utils.appid;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        z_uid: '',
        weappid: '',
        share_wxfx: true,
        share_wx_bg: false,
        share_img: true,
        ifName: true,
        st: "1",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        that.setData({
            // z_uid: wx.getStorageSync("z_uid"),
            weappid: options.weappid,
            z_uid: options.uid
        })
        let p = {
            m: 'share',
            uid: that.data.z_uid,
            weappid: that.data.weappid,
            flag: 0,
        }
        post(p).then(res => {
            if (res.r == 0) {
                let image_pyq = JSON.parse(res.result);
                that.setData({
                    title: image_pyq.title,
                    imageUrl: image_pyq.img_card,
                    ghid: image_pyq.ghid,
                    path: image_pyq.path

                });
                wx.setClipboardData({
                    data: res.doc,
                    success() {
                        wx.showToast({
                            title: '文案复制成功',
                            icon: 'success'
                        });
                    }
                });
            }
        });
    },
    // 选项框
    checkboxChange(e) {
        let that = this;
        if (typeof(e.detail.value[0]) != "undefined") {
            this.setData({
                st: "1",
            });
        } else {
            this.setData({
                st: "0",
            });
        }
    },
    share_wx_zz() {
        let that = this;
        that.setData({
            share_wxfx: true,
            share_img: true,
            ifName: true,
            share_wx_bg: false,
            image_pyq: ''
        })
    },
    lalal() {
        let that = this;
        wx.previewImage({
            current: that.data.image_pyq, // 当前显示图片的http链接   
            urls: [that.data.image_pyq]
        })
    },
    // showfx: function(res) {
    //     let that = this;
    //     this.setData({
    //         title: res.currentTarget.dataset.title,
    //         imageUrl: res.target.dataset.image,
    //         weappid: res.currentTarget.dataset.weappid,
    //         doc: res.currentTarget.dataset.doc,
    //         share_wxfx: false
    //     });
    //     wx.setClipboardData({
    //         data: that.data.doc,
    //         success() {
    //             wx.showToast({
    //                 title: '文案复制成功',
    //                 icon: 'success'
    //             });
    //         }
    //     })
    // },
    share_circle(rimg) {
        let that = this;
        that.setData({
            share_wx_bg: true,
            share_img: false
        })
        wx.showLoading({
            title: "加载中",
            mask: true,
            success: (result) => {}
        });

        let p = {
            m: 'share',
            uid: that.data.z_uid,
            weappid: that.data.weappid,
            ua: 'wechat',
            flag: 1,
            st: that.data.st
        }
        post(p).then(res => {
            let image_pyq = JSON.parse(res.result).toString()
            that.setData({
                image_pyq: image_pyq
            });
            that.baocuntp()
        })
    },
    baocuntp() {
        let that = this;
        wx.downloadFile({
            url: that.data.image_pyq,
            success: function(res) {
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function(data) {
                        wx.showModal({
                            title: '保存成功',
                            content: '文案复制成功，文案图片已自动生成并保存到您的相册，可进入朋友圈分享',
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {}
                            }
                        })
                    },
                    fail: function(res) {
                        if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                            wx.openSetting({
                                success(settingdata) {
                                    if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                                        // console.log("获取权限成功，再次点击图片保存到相册")
                                    } else {
                                        // console.log("获取权限失败")
                                    }
                                }
                            })
                        }
                    }
                })
            }
        });
    },
    imageLoading(ev) {
        if (ev.detail.height > 0 && ev.detail.width > 0) {
            wx.hideLoading();
            this.setData({
                ifName: false,
                share_img: false
            });
        }
    },
    share_circle_bc() {
        let that = this;
        that.baocuntp();
        // wx.downloadFile({
        //     url: that.data.image_pyq,
        //     success: function(res) {
        //         wx.saveImageToPhotosAlbum({
        //             filePath: res.tempFilePath,
        //             success: function(data) {
        //                 wx.showModal({
        //                     title: '保存成功',
        //                     content: '文案复制成功，文案图片已自动生成并保存到您的相册，可进入朋友圈分享',
        //                     showCancel: false,
        //                     success: function(res) {
        //                         if (res.confirm) {}
        //                     },
        //                     fail: function(res) {

        //                     }
        //                 })
        //             }
        //         })
        //     }
        // })
    },
    onShareAppMessage: function(res) {
        let that = this;
        var getPath = "";
        if (that.data.path.indexOf('?') != -1) {
            getPath = that.data.path + "&st=" + this.data.st;
        } else { // 不包含问号
            getPath = that.data + path + "?st=" + this.data.st;
        }
        if (res.from === 'button') {
            return {
                title: that.data.title,
                path: getPath,
                imageUrl: that.data.imageUrl,
                success: (res) => {}
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})